package com.xlh.controller.courseTest;

import com.xlh.common.Result;
import com.xlh.dto.PageDTO;
import com.xlh.dto.courseTest.GradeStatisticsListDTO;
import com.xlh.dto.courseTest.check.StudentBaseListDTO;
import com.xlh.dto.courseTest.check.StudentListDTO;
import com.xlh.exception.common.GlobalException;
import com.xlh.param.support.StudentQueryParam;
import com.xlh.pojo.courseTest.CourseTestPaper;
import com.xlh.security.UserInfoHolder;
import com.xlh.service.courseTest.GradeStatisticsService;
import com.xlh.service.courseTest.PaperAnswerSheetService;
import com.xlh.service.system.OperationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhr 2019/4/8
 */
@Api(tags = "GradeStatistics", description = "成绩统计")
@RestController
public class GradeStatisticsController {
    @Autowired
    private GradeStatisticsService service;
    @Autowired
    private PaperAnswerSheetService answerSheetService;
    @Autowired
    private UserInfoHolder userInfoHolder;
    @Autowired
    private OperationService operationService;

    @ApiOperation("获取联动列表")
    @PostMapping("/course/{courseId}/grade-statistics")
    @PreAuthorize("@permissionValidator.teacher")
    public Result<PageDTO<GradeStatisticsListDTO>> list(
            @PathVariable("courseId") Long courseId,
            @RequestBody StudentQueryParam param
    ) {
        return Result.success(service.list(param.getPageNum(), param.getPageSize(), courseId, param.getClassIdList(), param.getUserInfo()));
    }

    @ApiOperation("学生考试成绩列表")
    @GetMapping("/course/{courseId}/student/exam-statistics")
    @PreAuthorize("@permissionValidator.student")
    public Result<List<CourseTestPaper>> listStudentExam(@PathVariable("courseId") Long courseId) {
        return Result.success(answerSheetService.listStudentExam(courseId, userInfoHolder.getUserId()));
    }

    @ApiOperation("学生平时成绩列表")
    @GetMapping("/course/{courseId}/student/base-statistics")
    @PreAuthorize("@permissionValidator.student")
    public Result<List<StudentBaseListDTO>> listStudentBase(@PathVariable("courseId") Long courseId) {
        return Result.success(service.listStudentBase(courseId, userInfoHolder.getUserId()));
    }
    @ApiOperation("随堂练习排名")
    @GetMapping("/course/{courseId}/student/base-statistics/{chapterId}/{classId}")
    @PreAuthorize("@permissionValidator.student")
    public Result<List<StudentListDTO>> listStudentBaseOther(@PathVariable("courseId") Long courseId, @PathVariable("chapterId") Long chapterId, @PathVariable("classId") Long classId) {
        return Result.success(service.listStudentBaseOther(userInfoHolder.getUserId(),courseId, chapterId,classId));
    }
    @ApiOperation("随堂练习统计")
    @GetMapping("/course/{courseId}/student/base-statistics/{chapterId}/{classId}/Statistics")
    @PreAuthorize("@permissionValidator.teacher")
    public Result<List<StudentListDTO>> listStudentStatistics(@PathVariable("courseId") Long courseId, @PathVariable("chapterId") Long chapterId, @PathVariable("classId") Long classId, @RequestParam String pageSize, @RequestParam String currentPage) {
        return Result.success(service.listStudentBaseStatistics(null,courseId, chapterId,classId,pageSize.equals("null")?0:Integer.valueOf(pageSize),currentPage.equals("null")?0:Integer.valueOf(currentPage)));
    }
    @PostMapping("/course/{courseId}/grade-statistics/download")
    @ApiOperation("导出成绩统计")
    @PreAuthorize("@permissionValidator.teacher")
    public Result downloadSystemProblem(@PathVariable("courseId") Long courseId, @RequestBody StudentQueryParam param) {
        return service.outputList(courseId, param.getClassIdList(), param.getUserInfo());
    }
    @ApiOperation("随堂练习导出")
    @PostMapping("/course/{courseId}/export/practice")
    @PreAuthorize("@permissionValidator.teacher")
    public Result exportExcel(@PathVariable("courseId") Long courseId, @RequestParam String courseName, @RequestParam("chapterId") Long chapterId, @RequestParam("classId") Long classId , @RequestParam String chapterName, @RequestParam String className) {
        List<StudentListDTO> studentListDTOS = service.listStudentBaseStatistics(null, courseId, chapterId, classId, null, null);
        if (studentListDTOS!=null){
            service.download(studentListDTOS,courseName,chapterName,className);
        }else {
            throw new GlobalException("暂无学生提交信息");
        }
        return Result.success();
    }

    



}
